package com.example.demo11;

import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MockMvcBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.regex.Matcher;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@Configuration
public class SpringMvcTestLearning {
    private MockMvc mockMvc;
    @Autowired
    private WebApplicationContext webContext;
    @Before
    public void SetUpMockMvc(){
        mockMvc = MockMvcBuilders.webAppContextSetup(webContext).build();
    } //可以增加属性 //基本属性和附加属性
    @Test
    public void homePage() throws Exception {
        mockMvc.perform(MockMvcRequestBuilders.get("/scalaUser/123"))
                .andExpect(MockMvcResultMatchers.status().isOk()) //判断请求状态 //进行检测 Matchers
                .andExpect(MockMvcResultMatchers.view().name("user/index"))
                .andExpect(MockMvcResultMatchers.model().attributeExists("books"))
                .andExpect(MockMvcResultMatchers.model().attribute("book",
                        Matchers.is(Matchers.empty())));

    }
}
